package com.gun.management.module.raffle.controller.admin.lottery;

import com.gun.management.framework.common.enums.CommonStatusEnum;
import com.gun.management.module.raffle.convert.LotteryConvert;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import static com.gun.management.framework.common.pojo.CommonResult.success;

import com.gun.management.framework.excel.core.util.ExcelUtils;

//import com.gun.management.framework.operatelog.core.annotations.OperateLog;
//import static com.gun.management.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.gun.management.module.raffle.controller.admin.lottery.vo.*;
import com.gun.management.module.raffle.dal.dataobject.lottery.LotteryDO;
import com.gun.management.module.raffle.service.lottery.LotteryService;

@Tag(name = "管理后台 - 抽奖信息")
@RestController
@RequestMapping("/raffle/lottery")
@Validated
public class LotteryController {

    @Resource
    private LotteryService lotteryService;

    @PostMapping("/create")
    @Operation(summary = "创建抽奖信息")
    @PreAuthorize("@ss.hasPermission('raffle:lottery:create')")
    public CommonResult<Long> createLottery(@Valid @RequestBody LotterySaveReqVO createReqVO) {
        return success(lotteryService.createLottery(createReqVO));
    }

    @PutMapping("/update")
     @Operation(summary = "更新抽奖信息")
     @PreAuthorize("@ss.hasPermission('raffle:lottery:update')")
    public CommonResult<Boolean> updateLottery(@Valid @RequestBody LotterySaveReqVO updateReqVO) {
        lotteryService.updateLottery(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除抽奖信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('raffle:lottery:delete')")
    public CommonResult<Boolean> deleteLottery(@RequestParam("id") Long id) {
        lotteryService.deleteLottery(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得抽奖信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
     @PreAuthorize("@ss.hasPermission('raffle:lottery:query')")
    public CommonResult<LotteryRespVO> getLottery(@RequestParam("id") Long id) {
        LotteryDO lottery = lotteryService.getLottery(id);
        return success(BeanUtils.toBean(lottery, LotteryRespVO.class));
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('raffle:lottery:page')")
    public CommonResult<PageResult<LotteryRespVO>> getLotteryPage(@Valid LotteryPageReqVO pageReqVO) {
        PageResult<LotteryDO> pageResult = lotteryService.getLotteryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LotteryRespVO.class));
    }

    @GetMapping("/export-excel")
    @PreAuthorize("@ss.hasPermission('raffle:lottery:export')")
    public void exportLotteryExcel(@Valid LotteryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LotteryDO> list = lotteryService.getLotteryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "抽奖信息.xls", "数据", LotteryRespVO.class,
                        BeanUtils.toBean(list, LotteryRespVO.class));
    }


    @RequestMapping("/list")
    @PreAuthorize("@ss.hasPermission('raffle:lottery:list')")
    public CommonResult<List<LotteryRespVO>> getLotteryList() {
        List<LotteryDO> categoryList = lotteryService.getLotteryListByStatus(
                CommonStatusEnum.ENABLE.getStatus());
        categoryList.sort(Comparator.comparing(LotteryDO::getCreateTime)); // 按 sort 降序排列
        return success(LotteryConvert.INSTANCE.convertList04(categoryList));
    }


    @PutMapping("/updateStatusEnabled")
    @Operation(summary = "修改状态")
    @PreAuthorize("@ss.hasPermission('raffle:lottery:updateStatusEnabled')")
    public CommonResult<Boolean> updateStatusEnabled(@Valid @RequestBody LotteryEnabledReqVO updateReqVO) {
        lotteryService.updateStatusEnabled(updateReqVO.getId(), updateReqVO.getCategoryId(),updateReqVO.getEnabled());
        return success(true);
    }


}